package wec_wallet.ebo.com.activity.capital;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.activity.MyBaseActivity;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import wec_wallet.ebo.com.utils.URLs;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import bassproject.ebo.com.ebobass.utils.JsonT;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.adapter.AccountAdapter;
import wec_wallet.ebo.com.bean.Address;
import wec_wallet.ebo.com.common.Constants;
import wec_wallet.ebo.com.utils.Functions;

public class ManagerAccountActivity extends MyBaseActivity {

    private final int TOADDACTIVITY = 10001;
    @BindView(R.id.manager_account_img)
    ImageView managerAccountImg;
    @BindView(R.id.rv_manager_account)
    RecyclerView rv_manager_account;
    String coinname;
    /**
     * 获取地址列表
     */
    ArrayList<Address> addressArrayList = new ArrayList<>();
    @BindView(R.id.add_null)
    TextView addNull;
    private AccountAdapter accountAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_account);
        ButterKnife.bind(this);
        initViews();
    }

    @Override
    public void initViews() {
        findViewById(R.id.actionbar_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rv_manager_account.setLayoutManager(new LinearLayoutManager(this));
//        rv_manager_account.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        accountAdapter = new AccountAdapter(addressArrayList, this);
        rv_manager_account.setAdapter(accountAdapter);
        accountAdapter.setOnItemClickLitener(new AccountAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivityForResult(new Intent(ManagerAccountActivity.this, AccountAddressActivity.class)

                        .putExtra(Constants.WALLETID, addressArrayList.get(position)
                                .getCoin_name()).putExtra("address", addressArrayList.get(position)), TOADDACTIVITY);
            }
        });
        coinname = getIntent().getStringExtra(Constants.WALLETID);
        getAddressList();
    }

    private void getAddressList() {
        if (I_Share.isLogin()) {
            Map<String, Object> postParams = new HashMap<String, Object>();
            postParams.put("token", I_Share.readAccountToken());
            postParams.put("uid", I_Share.readAccountUserid());
            postParams.put("coinname", "WEC");
            Functions.post(this, URLs.ADDRESSLIST, postParams, new IHttpPostListener() {
                @Override
                public void onTimeOut() {

                }

                @Override
                public void responseSuccess(String result) {
                    if (Ipost.isTimeOut(result)) {
                        return;
                    }
                    try {
                        if (!TextUtils.isEmpty(result)) {
                            JsonT<ArrayList<Address>> json = JSON.parseObject(result, new TypeReference<JsonT<ArrayList<Address>>>() {
                            });
                            if (json.getIs_auth() != null && json.getIs_auth() == 0) {
                                Functions.relogin(ManagerAccountActivity.this);
                                return;
                            }
                            addressArrayList.clear();
                            addressArrayList.addAll(json.getData());
                            accountAdapter.notifyDataSetChanged();
                            if (addressArrayList == null || json.getData().size() == 0) {
                                addNull.setVisibility(View.VISIBLE);
                            } else {
                                addNull.setVisibility(View.GONE);
                            }
                        }
                    } catch (Exception e) {
                        IToast.show(getResources().getString(R.string.data_error));
                    }
                }
            }, true, false);
        } else {
            finish();
        }
    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.manager_account_img, R.id.add_null})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.manager_account_img:
                startActivityForResult(new Intent(ManagerAccountActivity.this, AccountAddressActivity.class)
                                .putExtra(Constants.WALLETID, coinname)
                        , TOADDACTIVITY);
                break;
            case R.id.add_null:
                startActivityForResult(new Intent(ManagerAccountActivity.this, AccountAddressActivity.class)
                                .putExtra(Constants.WALLETID, coinname)
                        , TOADDACTIVITY);
                break;
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (resultCode ==RESULT_OK){
        if (requestCode == TOADDACTIVITY) {
            getAddressList();
        }
//        }
    }


}
